package br.ufrn.engcomp.arq.negocio.validacao;

/**
 * 
 * @author Claudio
 *
 */
import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.util.ArrayList; 
import java.util.List; 

import br.ufrn.engcomp.arq.dominio.Entidade;
import br.ufrn.engcomp.arq.negocio.validacao.annotations.RequiredField;

public class RequiredFieldValidation { 
	private List<String> emptyFields; 
	
	public RequiredFieldValidation() { 
		emptyFields = new ArrayList<String>(); 
	} 
	
	@SuppressWarnings("rawtypes") 
	public boolean isEmpty(Entidade entity) { 
		clean(); 
		boolean empty = false; 
		Class entityClass = entity.getClass(); 
		Field[] fields = entityClass.getDeclaredFields(); 
		for (Field currentField:fields) { 
			try { 
				currentField.getAnnotation(RequiredField.class); 
				Object fieldValue = getField(entity, currentField.getName()); 
				if (fieldValue == null || fieldValue.equals("")) { 
					empty = true; 
					emptyFields.add(currentField.getName()); 
				} 
			}catch (Exception e) { 
				// do nothing yet				
			} 
		} 
		return empty; 
	} 
	
	public boolean isEmpty(String value) { 
		clean(); 
		boolean empty = false; 
		if (value == null || value.equals("") ) { 
			empty = true;
			emptyFields.add("Busca"); 
		} 
		return empty; 
	} 
	
	@SuppressWarnings({ "unchecked", "rawtypes" }) 
	private Object getField(Entidade entity, String fieldName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { 
		Class entityClass = entity.getClass(); 
		Method method = entityClass.getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1)); 
		return method.invoke(entity); 
	} 
	
	public void clean() { 
		emptyFields.clear(); 
	} 
	
	public List<String> getEmptyFields() { 
		return emptyFields; 
	} 
}
